import numpy as np
import matplotlib.pyplot as mp
import integral as inte

mp.clf()

f = lambda x: np.cos(x)

def convergence_tests(f, max_iterations, a, b):

    Y = np.zeros([1,max_iterations]) 
    Z = np.zeros([1,max_iterations])
    A = np.zeros([1,max_iterations])
    B = np.zeros([1,max_iterations])
    C = np.zeros([1,max_iterations])
    D = np.zeros([1,max_iterations])
    
    for i in np.arange(0, max_iterations, 1):
        Y[0][i] = inte.left_rectangle_method(f, i, a, b)
        Z[0][i] = inte.right_rectangle_method(f, i, a, b)
        A[0][i] = inte.middle_dot_method(f, i, a, b)
        B[0][i] = inte.left_trapezium_method(f, i, a, b)
        C[0][i] = inte.right_trapezium_method(f, i, a, b)
        D[0][i] = inte.call_simpson(f, i, a, b)

    
    return (Y[0], Z[0], A[0], B[0], C[0], D[0])

(Y,Z,A,B,C,D) = convergence_tests(f, 40, 0, 0.5)

y = mp.plot(Y)
z = mp.plot(Z)
a = mp.plot(A)
b = mp.plot(B)
c = mp.plot(C)
d = mp.plot(D)
mp.xlabel('Number of iterations')
mp.ylabel('Value')
mp.legend((y,z,a,b,c,d),("left rectangle","right rectangle","middle dot","left trapezium","right trapezium","simpson"))
mp.title('Integer value in function of the iterations number with cos between 0 and 0.5')
mp.savefig('Convergence of different methods.png')
mp.show()
